<template>
  <div class="articaldetail">
    <!-- 头部 -->
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span
          class="iconfont iconnew new"
          @click="$router.push({ name: 'index' })"
        ></span>
      </div>
      <span @click="useFollows" :class="{ active: articleDetail.has_follow }">{{
        articleDetail.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <!-- 文章内容 -->
    <div class="detail">
      <div class="title">{{ articleDetail.title }}</div>
      <div class="desc">
        <span>{{ articleDetail.user && articleDetail.user.nickname }}</span>
        &nbsp;&nbsp;
        <span>{{ articleDetail.create_date | dateFormat }}</span>
      </div>
      <div
        class="content"
        v-html="articleDetail.content"
        v-if="articleDetail.type == 1"
      ></div>
      <!-- src:路径
      controls:显示控制面板
      loop:循环播放
      autoplay:自动播放，在chrome没有效果
      poster:封面 -->
      <video
        :src="articleDetail.content"
        v-if="articleDetail.type == 2"
        controls
      ></video>
      <div class="opt">
        <span class="like">
          <van-icon name="good-job-o" @click="postLike" />{{
            articleDetail.like_length
          }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="../assets/logo.png" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <!-- 底部评论块 -->
    <commentFooter :post="articleDetail"></commentFooter>
  </div>
</template>

<script>
import commentFooter from "@/components/commentFooter.vue";
import { dateFormat } from "../utils/myfilter.js";
import { getArticleDetail } from "@/api/article.js";
import { user_follows, user_unfollow } from "@/api/user.js";
import { post_like } from "@/api/article.js";
export default {
  data() {
    return {
      articleDetail: {},
    };
  },
  filters: {
    dateFormat,
  },
  components: {
    commentFooter,
  },
  methods: {
    // 用户关注
    async useFollows() {
      let id = this.articleDetail.user.id;
      //   console.log(id);
      // 关键是根据当前关注的状态值进行处理，如果has_follow是true,则说明当前已经关注了，那么这次的单击是取消关注，否则就是关注
      let res;
      if (this.articleDetail.has_follow) {
        //   取消关注
        res = await user_unfollow(id);
      } else {
        // 关注;
        res = await user_follows(id);
      }

      this.articleDetail.has_follow = !this.articleDetail.has_follow;

      this.$toast.success(res.data.message);
    },
    // 点赞文章
    async postLike() {
      let res = await post_like(this.$route.params.id);
      // console.log(res);
      this.articleDetail.has_like = !this.articleDetail.has_like;
      this.$toast.success(res.data.message);
      if (res.data.message == "点赞成功") {
        this.articleDetail.like_length++;
      } else {
        this.articleDetail.like_length--;
      }
    },
  },

  async mounted() {
    //   根据文章id获取文章的详情，实现文章详情的动态渲染
    let id = this.$route.params.id;
    let res = await getArticleDetail(id);
    console.log(res);
    this.articleDetail = res.data.data;
    console.log(this.articleDetail);
  },
};
</script>

<style lang='less' scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active {
      background-color: transparent;
      border: 1px solid #ccc;
      color: #000;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
      display: block;
    }
  }
  video {
    width: 100%;
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  //   .like {
  //     &.active {
  //       color: red;
  //     }
  //   }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px 50px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>